package games.common;

import java.io.IOException;
import org.andengine.entity.scene.background.AutoParallaxBackground;
import org.andengine.entity.scene.background.ParallaxBackground.ParallaxEntity;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.TextureManager;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import android.content.Context;

public class Field {	
	/**
	 * Constants
	 */
	private static final int ATLAS_WIDTH = 1807;
	private static final int ATLAS_HEIGHT = 720;

	/**
	 * Fields
	 */
	private ITextureRegion textureCielo;
	private ITextureRegion textureColinas;
	private ITextureRegion textureSol;
	private ITextureRegion textureNubes;
	private ITextureRegion texturePiso;
	private AutoParallaxBackground background;	
	private float velCielo;
	private float velColinas;
	private float velSol;
	private float velNubes;
	private float velPiso;
	
	/**
	 * Constructor
	 */
	public Field() {
		super();
		this.velCielo = 0.0f;
		this.velColinas = -6.0f;
		this.velSol = 0.0f;
		this.velNubes = -1.0f;
		this.velPiso = -50.0f;
	}
	
	/**
	 * Methods
	 */
	public void onCreateResourcesBackground(TextureManager textureManager, Context context) throws IOException {
       BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("imagenes/");
		
		BitmapTextureAtlas atlasCielo = new BitmapTextureAtlas(textureManager,ATLAS_WIDTH, ATLAS_HEIGHT);
		this.textureCielo = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlasCielo, context, "cielo.png", 0, 0);
		atlasCielo.load();

		BitmapTextureAtlas atlasColinas = new BitmapTextureAtlas(textureManager,ATLAS_WIDTH, ATLAS_HEIGHT);
		this.textureColinas = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlasColinas, context, "colinas.png", 0, 0);
		atlasColinas.load();

		BitmapTextureAtlas atlasSol = new BitmapTextureAtlas(textureManager, ATLAS_WIDTH,ATLAS_HEIGHT);
		this.textureSol = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlasSol, context, "sol.png", 0, 0);
		atlasSol.load();

		BitmapTextureAtlas atlasNubes = new BitmapTextureAtlas(textureManager,ATLAS_WIDTH, ATLAS_HEIGHT);
		this.textureNubes = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlasNubes, context, "nubes.png", 0, 0);
		atlasNubes.load();

		BitmapTextureAtlas atlasPiso = new BitmapTextureAtlas(textureManager, ATLAS_WIDTH,	ATLAS_HEIGHT);
		this.texturePiso = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlasPiso, context, "piso.png", 0, 0);
		atlasPiso.load();
	}
	
	public void onCreateSceneBackground(VertexBufferObjectManager vertexBufferObjectManager) {		
		
		background = new AutoParallaxBackground(0, 0, 0, 10);
		
		final Sprite spriteCielo = new Sprite(0, 0, this.textureCielo,vertexBufferObjectManager);
		spriteCielo.setOffsetCenter(0, 0);
		background.attachParallaxEntity(new ParallaxEntity(this.velCielo,spriteCielo));

		final Sprite spriteColinas = new Sprite(0, 0, this.textureColinas,vertexBufferObjectManager);
		spriteColinas.setOffsetCenter(0, 0);
		background.attachParallaxEntity(new ParallaxEntity(this.velColinas,spriteColinas));

		final Sprite spriteSol = new Sprite(0, 0, this.textureSol,vertexBufferObjectManager);
		spriteSol.setOffsetCenter(0, 0);
		background.attachParallaxEntity(new ParallaxEntity(this.velSol,spriteSol));

		final Sprite spriteNubes = new Sprite(0, 0, this.textureNubes,vertexBufferObjectManager);
		spriteNubes.setOffsetCenter(0, 0);
		background.attachParallaxEntity(new ParallaxEntity(this.velNubes,spriteNubes));

		final Sprite spritePiso = new Sprite(0, 0, this.texturePiso,vertexBufferObjectManager);
		spritePiso.setOffsetCenter(0, 0);
		background.attachParallaxEntity(new ParallaxEntity(this.velPiso,spritePiso));
		
	}

	public AutoParallaxBackground getBackground() {
		return background;
	}
	
	public void menuScrollerMode(){
		this.velCielo = 0.0f;
		this.velColinas = 0.0f;
		this.velSol = 0.0f;
		this.velNubes = -8.0f;
		this.velPiso = 0.0f;
	}

}
